package com.hb.trade.api.routine.controller.center.sys;

import com.hb.core.pojo.center.sys.dto.NewsCenterDTO;
import com.hb.core.pojo.parent.PageResult;
import com.hb.trade.api.routine.util.SendNewsUtil;
import com.hb.trade.client.center.NewsCenterClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.auth.shiro.util.ShiroUtil;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * describe:
 *
 * @author zhongdl
 * @date 2022/02/18
 */
@RestController
@Slf4j
@Version(1.0)
@Api(tags = {"消息中心相关"})
@RequestMapping("/{version}")
public class ApiNewsCenterController {

    protected final NewsCenterClient newsCenterClient;

    public ApiNewsCenterController(NewsCenterClient newsCenterClient) {
        this.newsCenterClient = newsCenterClient;
    }


    @ApiOperation(value = "分页查询消息列表", notes = "分页查询消息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "newsType", value = "消息类型", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "readFlag", value = "是否已读", dataTypeClass = Integer.class),
    })
    @PostMapping("/newsCenter/pageNewsCenter")
    public ResponseDto pageNewsCenter(@RequestBody NewsCenterDTO newsCenter) {
        Integer roleSign = ShiroUtil.getExtInfo().get("roleSign", Integer.class);
        Long userId = ShiroUtil.getUserId();
        newsCenter.setReceiverId(userId);
        newsCenter.setRoleSign(roleSign);
        Message<PageResult<NewsCenterDTO>> newsCenterMsg = newsCenterClient.pageNewsCenter(newsCenter);
        return ResultUtil.msg(newsCenterMsg);
    }

    @ApiOperation(value = "批量标记为已读", notes = "批量标记为已读")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "id类型的数组", dataTypeClass = List.class),
    })
    @PostMapping("/newsCenter/updateBatchRead")
    public ResponseDto updateBatchRead(@RequestBody NewsCenterDTO newsCenter) {

        Message<Boolean> updateBatchRead = newsCenterClient.updateBatchRead(newsCenter);
        //推送消息
        if (updateBatchRead.isSuccess()){
            SendNewsUtil.pushWebSocket();
        }
        return ResultUtil.msg(updateBatchRead);
    }
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "id类型的数组", dataTypeClass = List.class),
    })
    @PostMapping("/newsCenter/removeBatch")
    public ResponseDto removeBatch(@RequestBody NewsCenterDTO newsCenter) {

        Message<Boolean> removeBatch = newsCenterClient.removeBatch(newsCenter);
        //推送消息
        if (removeBatch.isSuccess()){
            SendNewsUtil.pushWebSocket();
        }
        return ResultUtil.msg(removeBatch);
    }
    @ApiOperation(value = "已读和未到的消息数量", notes = "已读和未到的消息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "id类型的数组", dataTypeClass = List.class),
    })
    @PostMapping("/newsCenter/readCount")
    public ResponseDto readCount(@RequestBody NewsCenterDTO newsCenter) {
        Long userId = ShiroUtil.getUserId();
        Integer roleSign = ShiroUtil.getExtInfo().get("roleSign", Integer.class);

        newsCenter.setReceiverId(userId);
        newsCenter.setRoleSign(roleSign);
        Message<NewsCenterDTO> removeBatch = newsCenterClient.readCount(newsCenter);
        return ResultUtil.msg(removeBatch);
    }

    @PostMapping("/newsCenter/insertNewsCenter")
    public ResponseDto insertNewsCenter(@RequestBody NewsCenterDTO newsCenterDTO) {
        ShiroUtil.fillOwner(newsCenterDTO);
        Message<Boolean> newsCenter = newsCenterClient.insertNewsCenter(newsCenterDTO);
        return ResultUtil.msg(newsCenter);
    }


}
